package com.bank.service;

import com.bank.api.DollarBankTransferApi;
import com.bank.api.RMBBankTransferApi;
import com.bank.api.model.AmountExchange;
import com.bank.api.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.hmily.annotation.HmilyTCC;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TransferServiceImpl implements TransferService {

    @DubboReference
    private DollarBankTransferApi dollarBankTransferApi;

    @DubboReference
    private RMBBankTransferApi rmbBankTransferApi;


    @HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")
    public int transfer(AmountExchange amountExchange) {
        Response response1 = dollarBankTransferApi.transfer(amountExchange);
        Response response2 = rmbBankTransferApi.transfer(amountExchange);
        if (!response1.isSuccess() || !response2.isSuccess()) {
            return 0;
        }
        return 1;
    }


    /**
     * confirm 直接完成流水
     *
     * @param amountExchange
     * @return
     */
    public boolean confirm(AmountExchange amountExchange) {
        log.info("transfer confirm,{}", amountExchange.getTrxRecordNo());
        return Boolean.TRUE;
    }

    public boolean cancel(AmountExchange amountExchange) {
        log.info("transfer cancel,{}", amountExchange.getTrxRecordNo());
        return Boolean.TRUE;
    }
}
